package com.ebelter.nb.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.nb.R;


/**
 * desc:文本提示对话框
 */
public class Tip1Dialog extends Dialog implements View.OnClickListener {

    public static final int Gravity_CENTER = 1;
    public static final int Gravity_BUTTOM = 2;

    public int mGravity;
    private Context mContext;

    private TextView content;


    private Button positive_bt;
    private String tip;


    private void FV() {
        content =findViewById(R.id.tip1dialog_content);
        positive_bt = findViewById(R.id.tip1dialog_positionbt);
    }


    public void setTittleDrawableLeft(int res){
        if (content!=null){
            Drawable drawable=getContext().getResources().getDrawable(res);
            drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
            content.setCompoundDrawables(drawable,null,null,null);

        }
    }






    private void setListeners() {

        positive_bt.setOnClickListener(this);
    }

    public Tip1Dialog(Context context,String tip) {
        super(context, R.style.dialog);

//         <style name="dialog" parent="@android:style/Theme.Dialog">
//        <item name="android:windowFrame">@null</item>
//        <item name="android:windowIsFloating">true</item>
//        <item name="android:windowIsTranslucent">false</item>
//        <item name="android:windowNoTitle">true</item>
//        <item name="android:windowBackground">@android:color/transparent</item>
//        <item name="android:backgroundDimEnabled">true</item>
//        </style>

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setCanceledOnTouchOutside(false);
        this.mContext = context;
        this.tip = tip;
        mGravity=Gravity_CENTER;

    }

    public void setGravity(int gravity){
        this.mGravity=gravity;
    }



    public boolean isPingBiBack;

    public boolean dispatchKeyEvent(KeyEvent event){
        switch(event.getKeyCode()){
            case KeyEvent.KEYCODE_BACK:
                if (isPingBiBack){
                    LogUtils.i("TextTipDialog","------KeyEvent.KEYCODE_BACK");
                    return false;
                }

        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final View view = View.inflate(mContext, R.layout.tip1dialog_ly, null);
        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                Window dialogWindow = getWindow();
                if (dialogWindow != null) {
                    if (mGravity==Gravity_BUTTOM){
                        dialogWindow.setGravity(Gravity.BOTTOM);
                    }else if (mGravity==Gravity_CENTER){
                        dialogWindow.setGravity(Gravity.CENTER);
                    }

                    WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                    DisplayMetrics d = mContext.getResources().getDisplayMetrics();
                   // int maxOne = Math.max(d.widthPixels, d.heightPixels);
                    int minOne = Math.min(d.widthPixels, d.heightPixels);
                    lp.width = (int) (minOne * 0.82);
//                    lp.height = (int) (minOne * 0.3);
                   // lp.width= DipUtil.dip2px(getContext(),288);
                    lp.height= LinearLayout.LayoutParams.WRAP_CONTENT;
                    dialogWindow.setAttributes(lp);
                }
            }
        });
        setContentView(view);
        FV();
        setListeners();
        initDatas();
    }

    private void initDatas() {
        content.setText(tip);
    }





    private ICancelOkBtListener listener;

    public void setListener(ICancelOkBtListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
       if (v.getId() == R.id.tip1dialog_positionbt) {
            if (listener != null) {
                listener.ok(this);
            }
        }

    }

    public interface ICancelOkBtListener {
        void ok(Dialog dialogView);
    }


}
